﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace NHTrace.Tests
{
  [TestFixture]
  public class SqlParameterReplacerTests
  {
    [Test]
    public void ReplaceParametersTest_StatementWithParameters()
    {
      const string STR_SqlStatement = "NHibernate: INSERT INTO \"Product\" (Name, UnitPrice, Discontinued, Id) " +
        "VALUES (@p0, @p1, @p2, @p3);@p0 = 'Apple0, Apple1, Apple2', @p1 = 0.25, @p2 = False, @p3 = 1001";

      var parameters = new Dictionary<string, string>();
      parameters.Add("@p0", "'Apple0, Apple1, Apple2'");
      parameters.Add("@p1", "0.25");
      parameters.Add("@p2", "False");
      parameters.Add("@p3", "1001");

      var replacer = new SqlParameterReplacer();
      var newStatement = replacer.ReplaceParameters(STR_SqlStatement, parameters);

      Assert.AreEqual("NHibernate: INSERT INTO \"Product\" (Name, UnitPrice, Discontinued, Id) " +
        "VALUES ('Apple0, Apple1, Apple2', 0.25, False, 1001);", newStatement);
    }

    [Test]
    public void ReplaceParametersTest_StatementWithNoParameters()
    {
      const string STR_SqlStatement = "NHibernate: INSERT INTO \"Product\" (Name, UnitPrice, Discontinued, Id) " +
        "VALUES ('Apple0, Apple1, Apple2', 0.25, False, 1001);";

      var parameters = new Dictionary<string, string>();

      var replacer = new SqlParameterReplacer();
      var newStatement = replacer.ReplaceParameters(STR_SqlStatement, parameters);

      Assert.AreEqual(STR_SqlStatement, newStatement);
    }

    [Test]
    public void ReplaceParametersTest_StatementWithMoreThan10Parameters()
    {
      const string STR_SqlStatement = "NHibernate: INSERT INTO \"Product\" (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) " +
        "VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11);" + 
        "@p0 = 'p0', @p1 = 'p1', @p2 = 'p2', @p3 = 'p3', @p4 = 'p4', @p5 = 'p5', @p6 = 'p6', @p7 = 'p7', @p8 = 'p8', @p9 = 'p9', @p10 = 'p10', @p11 = 'p11'";

      var parameters = new Dictionary<string, string>();
      parameters.Add("@p0", "'p0'");
      parameters.Add("@p1", "'p1'");
      parameters.Add("@p2", "'p2'");
      parameters.Add("@p3", "'p3'");
      parameters.Add("@p4", "'p4'");
      parameters.Add("@p5", "'p5'");
      parameters.Add("@p6", "'p6'");
      parameters.Add("@p7", "'p7'");
      parameters.Add("@p8", "'p8'");
      parameters.Add("@p9", "'p9'");
      parameters.Add("@p10", "'p10'");
      parameters.Add("@p11", "'p11'");

      var replacer = new SqlParameterReplacer();
      var newStatement = replacer.ReplaceParameters(STR_SqlStatement, parameters);

      var expected = "NHibernate: INSERT INTO \"Product\" (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) " +
        "VALUES ('p0', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9', 'p10', 'p11');";

      Assert.AreEqual(expected, newStatement);
    }

    [Test]
    public void ReplaceParametersTest_StatementIsInvalidSql()
    {
      const string STR_SqlStatement = "some simple no sql text";

      var parameters = new Dictionary<string, string>();

      var replacer = new SqlParameterReplacer();
      var newStatement = replacer.ReplaceParameters(STR_SqlStatement, parameters);

      Assert.AreEqual(STR_SqlStatement, newStatement);
    }
  }
}
